﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    }
});
app.controller('NewsLetterController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {


    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add NewsLetter";
                $scope.Newsletter = null;
                $(".summernote").code("")
                break;
            case 'edit':
                $scope.title = "Edit News Letter";
                url = "Newsletter/GetNewsletter";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.Newsletter = data;
                    $scope.Newsletter.NewsLetterContent = $(".summernote").code(data.NewsLetterContent);
                });
                break;
        }
        $("#NewsLetter").modal({ backdrop: 'static' });
    }


    $scope.SendMail = function (statusmode, id) {
        $scope.id = id;
        $scope.sendstatus = statusmode;
        $scope.title = "Send Mail";
        $("#Mail").modal({ backdrop: 'static' });
    }

    $scope.ApproveRejectNewsLetter = function (status) {
        url = "Newsletter/ApproveRejectNewsletter?NewsletterId=" + $scope.id + "&Status=" + status;
        ApplicationService.Getdata(url).success(function (result) {
            alertboxservice.ShowMsg(result, "Mail ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

    $scope.SendNewsletters = function () {
        url = "Newsletter/SendNewsletter?NewsletterId=" + $scope.id;
        ApplicationService.Getdata(url).success(function (result) {
            alertboxservice.ShowMsg(result, "Mail ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }


    function BindGrid() {
        url = "Newsletter/GetNewsletters";
        ApplicationService.Getdata(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Newsletter/DeleteNewsletter";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }


    $scope.save = function (modelstate, id) {
        
        $scope.Newsletter.NewsLetterContent = $(".summernote").code();
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "Newsletter/AddNewsletter";
                break;
            case 'edit':
                url = "Newsletter/UpdateNewsletter";
                break;
        }
        ApplicationService.Save($scope.Newsletter, url).success(function (result) {
            alertboxservice.ShowMsg(result, "NewsLetter ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }
}]);

